<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>cup.cache &#8212; cup 3.2.33 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/alabaster.css" />
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../_static/sphinx_highlight.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
   
  <link rel="stylesheet" href="../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cup.cache</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    decorators related module</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">uuid</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">Queue</span> <span class="k">as</span> <span class="nn">queue</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">queue</span>
<span class="kn">import</span> <span class="nn">collections</span>
<span class="kn">import</span> <span class="nn">contextlib</span>

<span class="kn">import</span> <span class="nn">cup</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="kn">import</span> <span class="n">log</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="kn">import</span> <span class="n">err</span>
<span class="kn">from</span> <span class="nn">cup.util</span> <span class="kn">import</span> <span class="n">thread</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;CacheFull&#39;</span><span class="p">,</span> <span class="s1">&#39;KVCache&#39;</span><span class="p">,</span> <span class="s1">&#39;KvCache&#39;</span><span class="p">]</span>


<div class="viewcode-block" id="CacheFull"><a class="viewcode-back" href="../../cup.html#cup.cache.CacheFull">[docs]</a><span class="k">class</span> <span class="nc">CacheFull</span><span class="p">(</span><span class="n">err</span><span class="o">.</span><span class="n">BaseCupException</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    CacheFull for cache.KvCache</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="n">err</span><span class="o">.</span><span class="n">BaseCupException</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span></div>


<div class="viewcode-block" id="KVCache"><a class="viewcode-back" href="../../cup.html#cup.cache.KVCache">[docs]</a><span class="k">class</span> <span class="nc">KVCache</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Key-Value Cache object.</span>

<span class="sd">    You can use function set/get to access KeyValue Cache.</span>

<span class="sd">    When a k-v is hit by function **get**,</span>
<span class="sd">    the expire_sec will be expanded to 2 * (expire_sec)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_STAT</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;kvcache_stat&#39;</span><span class="p">,</span> <span class="s1">&#39;key_num expired_num&#39;</span>
    <span class="p">)</span>
    <span class="n">INFINITE_TIME</span> <span class="o">=</span> <span class="mi">10000</span> <span class="o">*</span> <span class="mi">365</span> <span class="o">*</span> <span class="mi">24</span> <span class="o">*</span> <span class="mi">60</span> <span class="o">*</span> <span class="mi">60</span> <span class="c1"># 10000 years, enough for cache</span>
    <span class="n">TIME_EXTENSION</span> <span class="o">=</span> <span class="mi">5</span> <span class="o">*</span> <span class="mi">60</span>   <span class="c1"># 5 mins</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">maxsize</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">time_extension</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param maxsize:</span>
<span class="sd">            0 by default which means store as more cache k/v as the system can</span>
<span class="sd">        :param time_extension:</span>
<span class="sd">            When a cache item has been hit, the expire_time will be refreshed</span>
<span class="sd">            to the greater one, either (TIME_EXTENSION + time.time() or</span>
<span class="sd">              (TIME_EXTENSION + expire_sec)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="o">=</span> <span class="s1">&#39;cache.noname.</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid4</span><span class="p">())</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                <span class="s1">&#39;You initialize the KVCache with no name. Strongly suggest&#39;</span>
                <span class="s1">&#39;you pick up a meaningful name for it in order to debug&#39;</span>
            <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sorted_keys</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">PriorityQueue</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="n">maxsize</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_maxsize</span> <span class="o">=</span> <span class="n">maxsize</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span> <span class="o">=</span> <span class="n">thread</span><span class="o">.</span><span class="n">RWLock</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">time_extension</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_time_extension</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">TIME_EXTENSION</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_time_extension</span> <span class="o">=</span> <span class="n">time_extension</span>

<div class="viewcode-block" id="KVCache.set_time_extension"><a class="viewcode-back" href="../../cup.html#cup.cache.KVCache.set_time_extension">[docs]</a>    <span class="k">def</span> <span class="nf">set_time_extension</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">time_extension</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;set time extension&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">time_extension</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;time extension should &gt; 0&#39;</span><span class="p">)</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;KVCache set time extension to </span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">time_extension</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_time_extension</span> <span class="o">=</span> <span class="n">time_extension</span></div>

    <span class="nd">@contextlib</span><span class="o">.</span><span class="n">contextmanager</span>
    <span class="k">def</span> <span class="nf">_lock_release</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">b_rw_lock</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">b_rw_lock</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="o">.</span><span class="n">acquire_writelock</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="o">.</span><span class="n">acquire_readlock</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">yield</span>
        <span class="c1"># pylint: disable=W0703</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;something happend in cache:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">error</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">b_rw_lock</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="o">.</span><span class="n">release_writelock</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="o">.</span><span class="n">release_readlock</span><span class="p">()</span>

<div class="viewcode-block" id="KVCache.set"><a class="viewcode-back" href="../../cup.html#cup.cache.KVCache.set">[docs]</a>    <span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">kvdict</span><span class="p">,</span> <span class="n">expire_sec</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set cache with kvdict</span>
<span class="sd">        ::</span>

<span class="sd">            {</span>
<span class="sd">                &#39;key1&#39;: &#39;value1&#39;,</span>
<span class="sd">                &#39;key2&#39;: &#39;value2&#39;,</span>
<span class="sd">                ....</span>
<span class="sd">            }</span>

<span class="sd">        :param kvdict:</span>
<span class="sd">            kvdict is a dict that contains your cache.</span>
<span class="sd">        :param expire_sec:</span>
<span class="sd">            if expire_sec is None, the cache will never expire.</span>

<span class="sd">        :return:</span>
<span class="sd">            True if set cache successfully. False otherwise.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">all</span><span class="p">([</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_maxsize</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">,</span>
                <span class="nb">len</span><span class="p">(</span><span class="n">kvdict</span><span class="p">)</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maxsize</span>
        <span class="p">]):</span>
            <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                <span class="s1">&#39;KVCache </span><span class="si">{0}</span><span class="s1"> cannot insert more &#39;</span>
                <span class="s1">&#39;elements than the maxsize&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_name</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="n">expire_value</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">expire_sec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">expire_sec</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">INFINITE_TIME</span><span class="p">:</span>
            <span class="n">expire_value</span> <span class="o">=</span> <span class="n">expire_sec</span> <span class="o">+</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">expire_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">INFINITE_TIME</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock_release</span><span class="p">(</span><span class="n">b_rw_lock</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">kvdict</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">:</span>
                    <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                        <span class="s1">&#39;KVCache: Key:</span><span class="si">{0}</span><span class="s1"> updated.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
                    <span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">expire_value</span><span class="p">,</span> <span class="n">kvdict</span><span class="p">[</span><span class="n">key</span><span class="p">])</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_heapq_newset</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">kvdict</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="n">expire_value</span><span class="p">):</span>
                    <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span></div>

    <span class="k">def</span> <span class="nf">_heapq_newset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">expire_value</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        headp set</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">any</span><span class="p">([</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_maxsize</span> <span class="o">==</span> <span class="mi">0</span><span class="p">,</span>
                <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_maxsize</span>
        <span class="p">]):</span>
            <span class="c1"># no limit, just insert it into the queue</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sorted_keys</span><span class="o">.</span><span class="n">put</span><span class="p">((</span><span class="n">expire_value</span><span class="p">,</span> <span class="n">key</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">expire_value</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># need replace the smallest one</span>
            <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">pop_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sorted_keys</span><span class="o">.</span><span class="n">get_nowait</span><span class="p">()</span>
                <span class="k">except</span> <span class="n">queue</span><span class="o">.</span><span class="n">Full</span><span class="p">:</span>
                    <span class="k">return</span> <span class="kc">False</span>
                <span class="n">real_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pop_value</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="kc">None</span><span class="p">)</span>
                <span class="c1"># key exipred, key deleted in self._kv_data</span>
                <span class="k">if</span> <span class="n">real_value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">expire_value</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_sorted_keys</span><span class="o">.</span><span class="n">put</span><span class="p">((</span><span class="n">expire_value</span><span class="p">,</span> <span class="n">key</span><span class="p">))</span>
                    <span class="k">return</span> <span class="kc">True</span>
                <span class="k">if</span> <span class="n">real_value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">pop_value</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
                    <span class="c1"># resort, adjust real</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_sorted_keys</span><span class="o">.</span><span class="n">put</span><span class="p">((</span><span class="n">expire_value</span><span class="p">,</span> <span class="n">key</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">expire_value</span> <span class="o">&lt;</span> <span class="n">pop_value</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
                        <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                            <span class="s1">&#39;KVCache </span><span class="si">{0}</span><span class="s1"> the alorithm you design has faults &#39;</span>
                            <span class="s1">&#39;the new inserted cache </span><span class="si">{1}</span><span class="s1"> expire time &#39;</span>
                            <span class="s1">&#39;&lt; the oldest cache </span><span class="si">{2}</span><span class="s1"> in it&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                                <span class="bp">self</span><span class="o">.</span><span class="n">_name</span><span class="p">,</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">expire_value</span><span class="p">),</span> <span class="n">pop_value</span>
                            <span class="p">)</span>
                        <span class="p">)</span>
                        <span class="k">return</span> <span class="kc">False</span>
                    <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">pop_value</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">expire_value</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_sorted_keys</span><span class="o">.</span><span class="n">put</span><span class="p">((</span><span class="n">expire_value</span><span class="p">,</span> <span class="n">key</span><span class="p">))</span>
                    <span class="k">break</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_get_refreshed_exipre_time</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">expire_sec</span><span class="p">):</span>
        <span class="n">new_refresh</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_time_extension</span>
        <span class="n">new_expire</span> <span class="o">=</span> <span class="n">expire_sec</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_time_extension</span>
        <span class="k">return</span> <span class="n">new_expire</span> <span class="k">if</span> <span class="n">new_expire</span> <span class="o">&lt;</span> <span class="n">new_refresh</span> <span class="k">else</span> <span class="n">new_refresh</span>

<div class="viewcode-block" id="KVCache.get"><a class="viewcode-back" href="../../cup.html#cup.cache.KVCache.get">[docs]</a>    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get your cache with key.</span>
<span class="sd">        If the cache is expired, it will return None.</span>
<span class="sd">        If the key does not exist, it will return None.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock_release</span><span class="p">(</span><span class="n">b_rw_lock</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">None</span>
            <span class="n">expire_sec</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">&gt;</span> <span class="n">expire_sec</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;KVCache </span><span class="si">{0}</span><span class="s1">: key </span><span class="si">{1}</span><span class="s1"> hit, but exipred </span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_name</span><span class="p">,</span> <span class="n">key</span>
                <span class="p">))</span>
                <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
                <span class="k">return</span> <span class="kc">None</span>
            <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;key:</span><span class="si">%s</span><span class="s1"> of kvCache fetched.&#39;</span> <span class="o">%</span> <span class="n">key</span><span class="p">)</span>
            <span class="n">expire_sec</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_refreshed_exipre_time</span><span class="p">(</span><span class="n">expire_sec</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">expire_sec</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">value</span></div>

<div class="viewcode-block" id="KVCache.pop_n_expired"><a class="viewcode-back" href="../../cup.html#cup.cache.KVCache.pop_n_expired">[docs]</a>    <span class="k">def</span> <span class="nf">pop_n_expired</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param num:</span>
<span class="sd">            if num is 0, will get all expired key/values</span>

<span class="sd">        :return:</span>
<span class="sd">            A dict.</span>
<span class="sd">            Return expired items. Return type is a dict</span>
<span class="sd">            ::</span>

<span class="sd">                {</span>
<span class="sd">                    &#39;key&#39; : (value, expire_time)</span>
<span class="sd">                }</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">kvlist</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">nowtime</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
        <span class="n">allexpire</span> <span class="o">=</span> <span class="kc">True</span> <span class="k">if</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">else</span> <span class="kc">False</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock_release</span><span class="p">(</span><span class="n">b_rw_lock</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">pop_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sorted_keys</span><span class="o">.</span><span class="n">get_nowait</span><span class="p">()</span>
                <span class="k">except</span> <span class="n">queue</span><span class="o">.</span><span class="n">Full</span><span class="p">:</span>
                    <span class="k">break</span>
                <span class="n">real_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pop_value</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="kc">None</span><span class="p">)</span>
                <span class="c1"># has already been deleted</span>
                <span class="k">if</span> <span class="n">real_value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="n">real_value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">pop_value</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
                    <span class="c1"># resort, adjust real</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_sorted_keys</span><span class="o">.</span><span class="n">put</span><span class="p">((</span><span class="n">real_value</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">pop_value</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">real_value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">nowtime</span><span class="p">:</span>
                        <span class="k">break</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">kvlist</span><span class="p">[</span><span class="n">pop_value</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span> <span class="o">=</span> <span class="n">real_value</span>
                        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">pop_value</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span>
                        <span class="k">if</span> <span class="ow">not</span> <span class="n">allexpire</span><span class="p">:</span>
                            <span class="n">num</span> <span class="o">-=</span> <span class="mi">1</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">allexpire</span> <span class="ow">and</span> <span class="n">num</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">break</span>
        <span class="k">return</span> <span class="n">kvlist</span></div>

<div class="viewcode-block" id="KVCache.size"><a class="viewcode-back" href="../../cup.html#cup.cache.KVCache.size">[docs]</a>    <span class="k">def</span> <span class="nf">size</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return:</span>
<span class="sd">            cached item size</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">)</span></div>

<div class="viewcode-block" id="KVCache.clear"><a class="viewcode-back" href="../../cup.html#cup.cache.KVCache.clear">[docs]</a>    <span class="k">def</span> <span class="nf">clear</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        remove all kv cache inside.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock_release</span><span class="p">(</span><span class="n">b_rw_lock</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sorted_keys</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_sorted_keys</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">PriorityQueue</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_maxsize</span><span class="p">)</span></div></div>


<span class="c1"># for compatibility</span>
<span class="n">KvCache</span> <span class="o">=</span> <span class="n">KVCache</span>
<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../index.html">cup</a></h1>








<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../cup.html">cup package</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../index.html">Documentation overview</a><ul>
  <li><a href="../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2023, CUP-DEV Team.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 7.0.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
      
    </div>

    

    
  </body>
</html>